//
//  ViewController.m
//  PopMenuTableView
//
//  Created by LoadingMx on 2017/9/18.
//  Copyright © 2017年 LoadingMx. All rights reserved.
//

#import "ViewController.h"
#import "MenuView.h"
#import "UIView+Extensions.h"

@interface ViewController ()

@property (nonatomic, assign) BOOL flag;
@property (nonatomic, assign) int itemCount;
@property (weak, nonatomic) IBOutlet UILabel *numberLabel;

@end

@implementation ViewController {
    NSArray *_dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    /**
       rightBarButton的点击标记，每次点击更改flag值。
       如果您用普通的button就不需要设置flag，通过按钮的seleted属性来控制即可
     */
    self.flag = YES;
    NSDictionary *dict1 = @{@"imageName" : @"icon_button_affirm",
                            @"itemName" : @"撤回"
                            };
    NSDictionary *dict2 = @{@"imageName" : @"icon_button_recall",
                            @"itemName" : @"确认"
                            };
    NSDictionary *dict3 = @{@"imageName" : @"icon_button_record",
                            @"itemName" : @"记录"
                            };
    
    NSArray *dataArray = @[dict1, dict2, dict3];
    _dataArray = dataArray;
    
    __weak typeof(&*self)weakSelf = self;
    
    [MenuView createMenuWithFrame:CGRectZero target:self dataArray:dataArray itemsClickBlock:^(NSString *str, NSInteger tag) {
        [weakSelf doSomething:str tag:tag];
    } backViewTap:^{
        weakSelf.flag = YES;
    }];
}

- (IBAction)addMenuItem:(UIButton *)sender {
    NSDictionary *addDict = @{@"imageName" : @"icon_button_recall",
                              @"itemName" : [NSString stringWithFormat:@"新增项 %d", self.itemCount + 1]
                              };
    NSArray *newItemArray = @[addDict];
    [MenuView appendMenuItemsWith:newItemArray];
    
    self.itemCount ++;
    self.numberLabel.text = [NSString stringWithFormat:@"累计增加 %d 项", self.itemCount];
    
    
}

- (IBAction)removeMenuItem:(UIButton *)sender {
    [MenuView updateMenuItemsWith:_dataArray];
    self.itemCount = 0;
    self.numberLabel.text = [NSString stringWithFormat:@"累计增加 %d 项", self.itemCount];
    
}

- (IBAction)popMenuAddClick:(UIBarButtonItem *)sender {
    
    [self popMenu:CGPointMake(25, 50)];
}

- (IBAction)popMenuAction:(UIBarButtonItem *)sender {
    [self popMenu:CGPointMake(65, 50)];
}
- (IBAction)popMenuCompose:(UIBarButtonItem *)sender {
    [self popMenu:CGPointMake(self.navigationController.view.width - 80, 50)];
}
- (IBAction)popMenuOrganize:(UIBarButtonItem *)sender {
    [self popMenu:CGPointMake(self.navigationController.view.width - 30, 50)];
}

- (void)popMenu:(CGPoint)point {
    
    if (self.flag) {
        [MenuView showMenuAtPoint:point];
        self.flag = NO;
    } else {
        [MenuView hidden];
        self.flag = YES;
    }
}

#pragma mark - 回调事件
- (void)doSomething:(NSString *)str tag:(NSInteger)tag {
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:str message:[NSString stringWithFormat:@"点击了第 %ld 个菜单", tag] preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *leftAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    UIAlertAction *rightAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
//    alertController.view.backgroundColor = [UIColor redColor];
    
    [alertController addAction:leftAction];
    [alertController addAction:rightAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    [MenuView hidden];
    self.flag = YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = touches.anyObject;
    CGPoint point = [touch locationInView:touch.view];
    [MenuView showMenuAtPoint:point];
}

- (void)dealloc {
    [MenuView clearMenu];
}

@end
